package com.goods.business.controller;


import com.goods.business.enums.InStockStatusEnum;
import com.goods.business.service.OutStockService;
import com.goods.common.error.BusinessException;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.OutStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/business/outStock")
public class BizOutStockController {
    @Autowired
    private OutStockService outStockService;

    @GetMapping("findOutStockList")
    public ResponseBean<PageVO<OutStockVO>> findOutStockList(@RequestParam Integer pageNum,
                                                             @RequestParam Integer pageSize,
                                                             OutStockVO outStockVO
    ) {
        PageVO<OutStockVO> list = outStockService.findOutStockList(pageNum, pageSize, outStockVO);
        return ResponseBean.success(list);
    }

    @GetMapping("detail/{id}")
    public ResponseBean<Map<String, Object>> detail(@RequestParam(defaultValue = "1") Integer pageNum,
                                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                                    @PathVariable Integer id) {
        Map<String, Object> res = outStockService.findInStockDetailList(pageNum, pageSize, id);
        return ResponseBean.success(res);
    }

    /**
     * 移到回收站
     *
     * @param id
     * @return
     */
    @PutMapping("remove/{id}")
    public ResponseBean remove(@PathVariable Long id) {
        outStockService.updateOutStockStatus(id, InStockStatusEnum.TRASH.getCode());
        return ResponseBean.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id) {
        outStockService.deleteOutStockById(id);
        return ResponseBean.success();
    }

    /**
     * 恢复
     *
     * @param id
     * @return
     */
    @PutMapping("back/{id}")
    public ResponseBean back(@PathVariable Long id) {
        outStockService.updateOutStockStatus(id, InStockStatusEnum.NORMAL.getCode());
        return ResponseBean.success();
    }

    /**
     * 出库
     */
    @PostMapping("addOutStock")
    public ResponseBean addOutStock(@RequestBody OutStockVO outStockVO) throws BusinessException {
        outStockService.addOutStock(outStockVO);
        return ResponseBean.success();
    }

    /**
     * 审核
     *
     * @param id
     * @return
     */
    @PutMapping("publish/{id}")
    public ResponseBean audit(@PathVariable Long id) throws BusinessException {
        outStockService.auditOutStock(id, InStockStatusEnum.NORMAL.getCode());
        return ResponseBean.success();
    }
}

